package com.mrent.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.Member;
import com.mrent.entity.OrderManager;
import com.mrent.util.Utils;
import com.mrent.web.controller.front.IndexController;

@Component
public class OrderManagerServiceImpl implements OrderManagerService {
	
	private static java.util.logging.Logger logger = Logger.getLogger(OrderManagerServiceImpl.class.getName());
	
	/**
	 * 保存管理员.
	 * 
	 * @param orderManager.
	 */
	public void saveOrderManager(OrderManager orderManager){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(orderManager);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 店主列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<OrderManager> listOrderManager(Page page){
		EntityManager em = EMF.getEM();
		String jpql = "select from OrderManager om ";
		Query query = em.createQuery(jpql);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<OrderManager> list = query.getResultList();
		return list;
	}
	
	/**
	 * 修改管理员.
	 * 
	 * @param member
	 */
	public void modifyOrderManager(OrderManager orderManager){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(orderManager);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally {	
			trans.commit();
		}
	}
	
	/**
	 * 根据订单号获取有效管理员.
	 * 
	 * @param orderNo
	 * @return
	 */
	public OrderManager getActivedByOrderNo(String orderNo){
		EntityManager em = EMF.getEM();
		String jpql = "select from OrderManager om where om.orderNo =:orderNo and om.state = 2";
		OrderManager orderManager = null;
		try{
			Query query = em.createQuery(jpql);
			query.setParameter("orderNo", orderNo);
			List<Member> list = query.getResultList();
			if(list.size() <= 1){
				orderManager = (OrderManager)query.getSingleResult();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return orderManager;
	}
	
	/**
	 * 根据会员名及日期获取生效的店主.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public List<OrderManager> getManagerByMemberDate(String orderMember,Integer year,Integer month,Integer day){
		EntityManager em = EMF.getEM();
		
		/*String jpql = "select from OrderManager o where o.orderRequestYear <= :year and o.orderEndYear > :year and o.orderRequestMonth <= :month " +
		"and o.orderEndMonth > :month and o.orderRequestDay <= :day  and o.orderEndDay > :day and o.orderState < 3";*/
		
		String jpql = "select from OrderManager o " +
				"where o.state = 2 and o.orderMember=:orderMember";
		Query query = em.createQuery(jpql);
		/*query.setParameter("year", year);
		query.setParameter("month", month);
		query.setParameter("day", day);*/
		query.setParameter("orderMember", orderMember);
		List<OrderManager> list = query.getResultList();
		List<OrderManager> bakList = new ArrayList<OrderManager>();
		for(OrderManager order:list){
			if((order.getOrderRequestYear() <= year && order.getOrderEndYear() >= year 
					&& order.getOrderRequestMonth() <= month && order.getOrderEndMonth() >= month 
					&& order.getOrderRequestDay() <= day && order.getOrderEndDay() >= day
					)||(order.getOrderRequestYear()>=year && order.getOrderRequestMonth() >=month)){
				bakList.add(order);
			}
		}
		return bakList;
	}
	
	/**
	 * 根据日期获取生效的店主.
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public List<OrderManager> getManagerByDate(Integer year,Integer month,Integer day){
		EntityManager em = EMF.getEM();
		
		/*String jpql = "select from OrderManager o where o.orderRequestYear <= :year and o.orderEndYear > :year and o.orderRequestMonth <= :month " +
		"and o.orderEndMonth > :month and o.orderRequestDay <= :day  and o.orderEndDay > :day and o.orderState = 2";*/
		
		String jpql = "select from OrderManager o " +
				"where o.state = 2";
		Query query = em.createQuery(jpql);
		/*query.setParameter("year", year);
		query.setParameter("month", month);
		query.setParameter("day", day);*/
		List<OrderManager> list = query.getResultList();
		List<OrderManager> bakList = new ArrayList<OrderManager>();
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		calendar.set(year, month, day);
		calendar.set(Calendar.MILLISECOND, 0);
		logger.info("before list:" +list.size());
		for(OrderManager order:list){
			Calendar dbcalendarstart = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
			dbcalendarstart.set(order.getOrderRequestYear(), order.getOrderRequestMonth(),order.getOrderRequestDay());
			dbcalendarstart.set(Calendar.MILLISECOND, 0);
			
			Calendar dbcalendarend = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
			dbcalendarend.set(order.getOrderEndYear(), order.getOrderEndMonth(), order.getOrderEndDay());
			dbcalendarend.set(Calendar.MILLISECOND, 0);
			if(Utils.orderManagerCalendarComp(calendar,dbcalendarstart,dbcalendarend)){
				bakList.add(order);
			}
		}
		logger.info("after list:" + bakList.size());
		return bakList;
	}
	
	/**
	 * 根据订单号获取有效管理员.
	 * 
	 * @param orderNo
	 * @return
	 */
	public OrderManager getByOrderNo(String orderNo){
		EntityManager em = EMF.getEM();
		String jpql = "select from OrderManager om where om.orderNo =:orderNo";
		OrderManager orderManager = null;
		try{
			Query query = em.createQuery(jpql);
			query.setParameter("orderNo", orderNo);
			List<Member> list = query.getResultList();
			if(list.size() <= 1){
				orderManager = (OrderManager)query.getSingleResult();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return orderManager;
	}
	
	

}
